<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>注册页</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="/layui/css/layui.css" rel="stylesheet">
</head>
<body>
<style>
    .demo-login-container{width: 320px; margin: 10px auto;}
    .demo-login-other .layui-icon{position: relative; display: inline-block; margin: 0 2px; top: 2px; font-size: 26px;}
</style>
<form class="layui-form">
    <div class="demo-login-container">
        <div class="layui-form-item" style="font-size:25px;text-align: center; color:#16b777">修改密码</div>
        <div class="layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-username"></i>
                </div>
                <input type="password" name="oldpassword" value="" lay-verify="required" placeholder="请输入原始密码" lay-reqtext="请输入原始密码" autocomplete="off" class="layui-input" lay-affix="clear">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-password"></i>
                </div>
                <input type="password" name="newpassword" value="" lay-verify="required" placeholder="请输入新密码" lay-reqtext="请输入新密码" autocomplete="off" class="layui-input" lay-affix="eye">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-password"></i>
                </div>
                <input type="password" name="confirmpassword" value="" lay-verify="required" placeholder="请再次输入新密码" lay-reqtext="请输入新密码" autocomplete="off" class="layui-input" lay-affix="eye">
            </div>
        </div>
        <div class="layui-form-item">
            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="update-pwd">修改密码</button>
        </div>
    </div>
</form>
<!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
<script src="/layui/layui.js"></script>
<script>
    layui.use(function(){
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.$;


        // 提交事件
        form.on('submit(update-pwd)', function(data){
            var field = data.field; // 获取表单字段值

            // 判定两次密码是否一致
            let  newPassword = field.newpassword
            let  confirmPassword = field.confirmpassword
            //原始密码
            let  oldPassword = field.oldpassword

            if(newPassword!=confirmPassword){
                layer.alert("两次密码不一致");
                return  false;
            }

            let username = '${user.username}'
            // 此处可执行 Ajax 等操作
            $.ajax({
                url:'/user?method=updatePassword',   //  后台接口
                type:'post',  //  请求方式
                data:{oldPassword,newPassword,username},  // 请求时需要携带的数据
                dataType:'JSON', // 响应数据格式
                success:function (res) {    //  请求成功回调函数     res  -> 接口的响应结果
                    if(res.code==0){
                        layer.msg(res.msg,{icon:1,time:3000},function () {
                            // 关闭弹窗
                            var index = parent.layer.getFrameIndex(window.name); // 先得到当前 iframe 层的索引
                            parent.layer.close(index); // 再执行关闭
                        })
                    }else {
                        layer.msg(res.msg,{icon:2})
                    }
                }
            })
            return false; // 阻止默认 form 跳转
        });
    });
</script>
</body>
</html>

